package org.mspring.platform.api.rss.writers.defaults;

import java.util.Iterator;
import java.util.List;

import org.mspring.platform.api.rss.elements.Channel;
import org.mspring.platform.api.rss.elements.Image;
import org.mspring.platform.api.rss.elements.Item;
import org.mspring.platform.api.rss.elements.TextInput;
import org.mspring.platform.api.rss.writers.GenericRSSWriter;

import electric.xml.Document;
import electric.xml.Element;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月26日
 */
@SuppressWarnings({ "rawtypes" })
public class RSS100 extends GenericRSSWriter {
    public String getFeed(Channel paramChannel) {
        Document localDocument = new Document();
        Element localElement1 = localDocument.newRoot();
        localElement1.setNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        localElement1.setNamespace("dc", "http://purl.org/dc/elements/1.1/");
        localElement1.setNamespace("", "http://purl.org/rss/1.0/");
        localElement1.setName("rdf", "RDF");
        Element localElement2 = localElement1.addElement("channel");
        GenericRSSWriter.addNSAttribute(localElement2, "rdf", "about", paramChannel.getAbout(), true);
        GenericRSSWriter.addElement(localElement2, "description", paramChannel.getDescription(), true);
        GenericRSSWriter.addElement(localElement2, "title", paramChannel.getTitle(), true);
        GenericRSSWriter.addElement(localElement2, "link", paramChannel.getLink(), true);
        GenericRSSWriter.addDublinCore(localElement2, paramChannel);
        Image localImage = paramChannel.getImage();
        TextInput localTextInput = paramChannel.getTextInput();
        List localList = paramChannel.getItems();
        Object localObject;
        if (localImage != null) {
            addImageRef(localElement2, localImage, "resource");
            localObject = addImageRef(localElement1, localImage, "about");
            GenericRSSWriter.addElement((Element) localObject, "title", localImage.getTitle(), false);
            GenericRSSWriter.addElement((Element) localObject, "link", localImage.getLink(), false);
            GenericRSSWriter.addElement((Element) localObject, "url", localImage.getUrl(), false);
            GenericRSSWriter.addDublinCore((Element) localObject, localImage);
        }
        if (localTextInput != null) {
            addTextInputRef(localElement2, localTextInput, "resource");
            localObject = addTextInputRef(localElement1, localTextInput, "about");
            GenericRSSWriter.addElement((Element) localObject, "title", localTextInput.getTitle(), true);
            GenericRSSWriter.addElement((Element) localObject, "link", localTextInput.getLink(), true);
            GenericRSSWriter.addElement((Element) localObject, "description", localTextInput.getDescription(), true);
            GenericRSSWriter.addElement((Element) localObject, "name", localTextInput.getName(), true);
            GenericRSSWriter.addDublinCore((Element) localObject, localTextInput);
        }
        addItemsRef(localElement2, localList);
        if ((localList != null) && (localList.size() > 0)) {
            localObject = localList.iterator();
            while (((Iterator) localObject).hasNext()) {
                Item localItem = (Item) ((Iterator) localObject).next();
                Element localElement3 = localElement1.addElement("item");
                GenericRSSWriter.addNSAttribute(localElement3, "rdf", "about", localItem.getAbout(), true);
                GenericRSSWriter.addElement(localElement3, "title", localItem.getTitle(), true);
                GenericRSSWriter.addElement(localElement3, "link", localItem.getLink(), true);
                GenericRSSWriter.addElement(localElement3, "description", localItem.getDescription(), false);
                GenericRSSWriter.addDublinCore(localElement3, localItem);
            }
        }
        return (String) localDocument.toString();
    }

    private void addItemsRef(Element paramElement, List paramList) {
        Element localElement1 = paramElement.addElement("items");
        Element localElement2 = localElement1.addElement("rdf", "Seq");
        if ((paramList != null) && (paramList.size() > 0)) {
            Iterator localIterator = paramList.iterator();
            while (localIterator.hasNext()) {
                Item localItem = (Item) localIterator.next();
                Element localElement3 = localElement2.addElement("li");
                GenericRSSWriter.addNSAttribute(localElement3, "rdf", "resource", localItem.getAbout(), true);
            }
        }
    }

    private Element addTextInputRef(Element paramElement, TextInput paramTextInput, String paramString) {
        Element localElement = null;
        if (paramTextInput != null) {
            localElement = paramElement.addElement("textinput");
            GenericRSSWriter.addNSAttribute(localElement, "rdf", paramString, paramTextInput.getAbout(), true);
        }
        return localElement;
    }

    private Element addImageRef(Element paramElement, Image paramImage, String paramString) {
        Element localElement = null;
        if (paramImage != null) {
            localElement = paramElement.addElement("image");
            GenericRSSWriter.addNSAttribute(localElement, "rdf", paramString, paramImage.getAbout(), true);
        }
        return localElement;
    }
}